package com.example.medicalequipment.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.medicalequipment.R;

import java.util.List;

public class DeviceCategoryAdapter extends BaseAdapter {

    private Context context;
    private List<String> categoryList;
    private int selectedPosition = 0; // 默认选中第一个分类

    public DeviceCategoryAdapter(Context context, List<String> categoryList) {
        this.context = context;
        this.categoryList = categoryList;
    }

    public void setSelectedPosition(int position) {
        this.selectedPosition = position;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return categoryList.size();
    }

    @Override
    public Object getItem(int position) {
        return categoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_device_category, parent, false);
            holder = new ViewHolder();
            holder.tvCategory = convertView.findViewById(R.id.tvCategory);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String category = categoryList.get(position);
        holder.tvCategory.setText(category);

        // 设置选中状态
        if (position == selectedPosition) {
            convertView.setBackgroundColor(context.getResources().getColor(R.color.primary));
            holder.tvCategory.setTextColor(context.getResources().getColor(android.R.color.white));
        } else {
            convertView.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
            holder.tvCategory.setTextColor(context.getResources().getColor(android.R.color.black));
        }

        return convertView;
    }

    private static class ViewHolder {
        TextView tvCategory;
    }
}
